<?php

namespace app\admin\controller\system;

use app\admin\controller\Controllers;
use app\admin\service\system\System as SystemService;
use app\admin\model\system\System as SystemModels;

class System extends Controllers
{
    /**
     * 显示资源列表
     * @param SystemService $service
     * @return \think\Response
     */
    public function index(SystemService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\system\System.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);

        return $this->fetch('system/system/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch('system/system/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(SystemService $service)
    {
        $post = $this->request->only(['title','key','value'], 'post');
        $this->validate($post, 'app\admin\validate\system\System.create');
        return $service->create($post)?
            success():
            failed('添加失败');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(SystemModels $model)
    {
        $this->assign([
            'data' => $model
        ]);
        return $this->fetch('system/system/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(SystemModels $model, SystemService $service)
    {
        $put = $this->request->only(['title','key','value'], 'put');
        $this->validate($put, 'app\admin\validate\foods\Foods.update');
        return $service->save($model, $put)?
            success():
            failed('编辑失败');
    }
}
